package com.cb2.algorithm.leetcode;

/**
 * <a href='https://leetcode.cn/problems/transpose-matrix'>转置矩阵(Transpose Matrix)</a>
 * <p>给你一个二维整数数组 matrix， 返回 matrix 的 转置矩阵 。</p>
 * <p>矩阵的 转置 是指将矩阵的主对角线翻转，交换矩阵的行索引与列索引。</p>
 *
 * <p>
 * <b>示例：</b>
 * <pre>
 *  示例 1：
 *      输入：matrix = [[1,2,3],[4,5,6],[7,8,9]]
 *      输出：[[1,4,7],[2,5,8],[3,6,9]]
 *
 *  示例 2：
 *      输入：matrix = [[1,2,3],[4,5,6]]
 *      输出：[[1,4],[2,5],[3,6]]
 * </pre>
 * </p>
 *
 * <p>
 * <b>提示：</b>
 * <ul>
 *     <li>m == matrix.length</li>
 *     <li>n == matrix[i].length</li>
 *     <li>1 <= m, n <= 1000</li>
 *     <li>1 <= m * n <= 10^5</li>
 *     <li>-10^9 <= matrix[i][j] <= 10^9</li>
 * </ul>
 * </p>
 *
 * @author c2b
 * @since 2025/4/28 16:38
 */
public class LC0867TransposeMatrix_S {
    static class Solution {
        public int[][] transpose(int[][] matrix) {
            int m = matrix.length;
            int n = matrix[0].length;
            int[][] ints = new int[n][m];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    ints[j][i] = matrix[i][j];
                }
            }
            return ints;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        for (int[] ints : solution.transpose(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}})) {
            Printer.printArrayInt(ints);
        }
        for (int[] ints : solution.transpose(new int[][]{{1, 2, 3}, {4, 5, 6}})) {
            Printer.printArrayInt(ints);
        }
    }
}
